<template>
  <div class="app-container">
    <el-dialog :model-value="itemsVisible" title="选择二级项目" :append-to-body="true" width="80%" height="1000px" @close="closeBtn">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item prop="code" label="项目编号">
          <el-input v-model="listQuery.code" placeholder="项目编号" />
        </el-form-item>
        <el-form-item prop="name" label="项目名称">
          <el-input v-model="listQuery.name" placeholder="项目名称" />
        </el-form-item>
        <el-form-item prop="startTime" label="年份年度">
          <el-input v-model="listQuery.startTime" placeholder="年份年度" />
        </el-form-item>
        <el-form-item prop="viability" label="存续状态">
          <el-select v-model="listQuery.viability" class="m-2" placeholder="请选择">
            <el-option key="" label="全部" value="" />
            <el-option v-for="item in getDictList('BudgetProjectViability')" :key="item.key" :label="`${item.key} - ${item.value}`" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item prop="ysySync" label="预算域同步状态">
          <el-select v-model="listQuery.ysySync" class="m-2" placeholder="请选择" :disabled="!isNullOrUndefinedOrEmpty(props.ysySync)">
            <el-option key="" label="全部" value="" />
            <el-option v-for="item in getEnumList('BudgetProjectTwoSync')" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item prop="state" label="状态">
          <el-select v-model="listQuery.state" class="m-2" placeholder="请选择" :disabled="!isNullOrUndefinedOrEmpty(props.state)">
            <el-option key="" label="全部" value="" />
            <el-option v-for="item in getEnumList('BudgetProjectTwoState')" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button v-waves type="success" icon="Search" @click="handleFilter">
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <div class="app-container">
        <el-table v-loading="listLoading"
                  :data="projectTwoList"
                  :max-height="695 +'px'"
                  border
                  fit
                  highlight-current-row
                  tooltip-effect="dark"
                  style="width: 100%;margin-bottom: 20px">
          <el-table-column align="center" type="index" label="序号" width="60px" />
          <el-table-column align="center" label="二级项目编码" prop="code" min-width="155px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ row.code }}</template>
          </el-table-column>
          <el-table-column align="center" label="二级项目名称" prop="name" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ row.name }}</template>
          </el-table-column>
          <el-table-column align="center" label="项目编号" prop="code" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ row.code }}</template>
          </el-table-column>
          <el-table-column align="center" label="项目来源" prop="source" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}"><DictTag :dict-list="getDictList('BudgetProjectSource')" :dict-key="row.source" /></template>
          </el-table-column>
          <el-table-column align="center" label="项目属性" prop="attribute" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}"><DictTag :dict-list="getDictList('BudgetProjectAttribute')" :dict-key="row.attribute" /></template>
          </el-table-column>
          <el-table-column align="center" label="起始年度" prop="startTime" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ row.startTime }}</template>
          </el-table-column>
          <el-table-column align="center" label="终止年度" prop="endTime" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ row.endTime }}</template>
          </el-table-column>
          <el-table-column align="center" label="最后修改时间" width="100px" prop="updateTime" />
          <el-table-column label="选择二级项目" width="150" align="center">
            <template v-slot="{row}">
              <el-radio :label="row.id" v-model="tableRadio" border @change="handleCurrentChange($event, row)">选择</el-radio>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-model:total="total" v-model:page="listQuery.pageNum" v-model:limit="listQuery.pageSize" @pagination="getList" class="pagination-model" />
        <div style="text-align:right;margin: 1.875rem 0;">
          <el-button type="danger" @click="closeBtn">关闭</el-button>
          <el-button type="primary" @click="oSrueBtn">确定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import {ref, watch} from 'vue'
import {doGet} from "@/config/http";
import {svc, getDictList, getEnumList, alertErrorMsg, isNullOrUndefinedOrEmpty} from "@/config/common";

const props = defineProps({
  itemsVisible: {
    type: Boolean,
    default: false,
  },
  ysySync: {
    type: String,
    default: null,
  },
  state: {
    type: String,
    default: null,
  },
})

let search = ref('')
let tableRadio = ref('')
let projectTwoList = ref([])
let listLoading = ref(true)
let total = ref(0)
let radioRow = null

let listQuery = ref({
  pageNum: 1,
  pageSize: 10,
  sortField: 'id',
  sortType: 'desc',
  projectOneId: null,
  code: null,
  name: null,
  viability: null,
  source: null,
  attribute: null,
  totalFund: null,
  putinFund: null,
  currFund: null,
  useGovId: null,
  applyGovId: null,
  previousArrange: null,
  managerOrgId: null,
  managerName: null,
  managerUserContact: null,
  workGovId: null,
  workUserId: null,
  workUserContact: null,
  state: null,
  startTime: null,
  endTime: null,
  isDebenture: null,
  isScientific: null,
  isConstruction: null,
  constructionDesc: null,
  isAgent: null,
  agentGovId: null,
  scientificDesc: null,
  scientificType: null,
  isGovernmentPurchase: null,
  purchaseProject: null,
  purchaseKind: null,
  purchaseRequire: null,
  purchaseDateStart: null,
  purchaseDateEnd: null,
  purchaseAttr: null,
  purchaseFund: null,
  purchasePrice: null,
  purchaseNum: null,
  purchaseUnit: null,
  ysySync: null,
  createTimeStart: null,
  createTimeEnd: null,
  updateTimeStart: null,
  updateTimeEnd: null,
})

const emit = defineEmits(['close', 'projectTwoSelect'])

watch(() => [props.itemsVisible, props.ysySync, props.state], async vals => {
  let visible = vals[0]
  let sync = vals[1]
  let state = vals[2]
  if (sync) {
    listQuery.value.ysySync = sync
  }
  if (state) {
    listQuery.value.state = state
  }
  if (visible === true) {
    await getList()
  }
})

// 关闭弹层
function closeBtn() {
  emit('close')
}

// 选择二级项目
function handleCurrentChange(val, row) {
  radioRow = row
}

// 选择二级点确定
function oSrueBtn() {
  if (!radioRow) {
    alertErrorMsg('请选择二级项目')
    return
  }
  emit('projectTwoSelect', radioRow)
  emit('close')
}

// 获取数据
async function getList() {
  listLoading.value = true
  let res = await doGet(svc, '/budget-project-two/page', listQuery.value)
  listLoading.value = false
  total.value = parseInt(res['total'])
  projectTwoList.value = res['records']
}

// 查询
async function handleFilter() {
  listQuery.value['pageNum'] = 1
  await getList()
}
</script>
<style>
.pagination-model {
  right: 36px !important;
}
</style>
